package com.example.javatest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javatest.entity.domain.SupSubmit;
import com.example.javatest.entity.domain.TbUser;
import com.example.javatest.mapper.TbUserMapper;
import com.example.javatest.service.SupSubmitService;
import com.example.javatest.mapper.SupSubmitMapper;
import com.example.javatest.service.TbUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author cac_qin
* @description 针对表【sup_submit】的数据库操作Service实现
* @createDate 2023-12-08 09:36:03
*/
@Service
public class SupSubmitServiceImpl extends ServiceImpl<SupSubmitMapper, SupSubmit>
    implements SupSubmitService{

    @Resource
    private TbUserService tbUserService;

    @Resource
    private SupSubmitMapper supSubmitMapper;

    @Resource
    private TbUserMapper tbUserMapper;

    @Override
    public boolean supHandle(String isHandle, int id) {

        SupSubmit supSubmit = supSubmitMapper.selectById(id);
        if(supSubmit == null){
            //查询不到该请求
            return false;
        }
        int userId = supSubmit.getUserId();
        TbUser tbUser = tbUserMapper.selectById(userId);
        if(tbUser == null){
            //查询不到该用户
            return false;
        }

        if(isHandle.equals("NO")){
            supSubmit.setSupHandle("被驳回");
            tbUser.setUserIdentity("user");
            supSubmitMapper.updateById(supSubmit);
            tbUserMapper.updateById(tbUser);
        }
        if(isHandle.equals("YES")){
            supSubmit.setSupHandle("已通过");
            tbUser.setUserIdentity(supSubmit.getSupIdentity());
            supSubmitMapper.updateById(supSubmit);
            tbUserMapper.updateById(tbUser);
        }
        return true;
    }

    @Override
    public boolean submit(String code, String role, int userId) {
        //查询申请人是否已经注册
        QueryWrapper<TbUser> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_Id",userId);
        int count = tbUserMapper.selectCount(queryWrapper);
        TbUser tbUser = tbUserService.getById(userId);
        if(count<0){
            return false;
        }

        //插入数据
        SupSubmit supSubmit=new SupSubmit();
        supSubmit.setUserId(userId);
        supSubmit.setSupIdentity(role);
        supSubmit.setSupCode(code);
        supSubmit.setSupName(tbUser.getUserName());
        boolean saveResult = this.save(supSubmit);
        if(!saveResult){
            return false;
        }
        return true;
    }
}




